<?php
ini_set("display_errors", "On");
error_reporting(E_ALL);

// 引入数据库连接文件
require_once '../coon/connection.php';

$id = $_POST['editID'];
$editName = $_POST['editName'];

// 查询语句
$querySql = "select id from greeting_card_element where id='$id'";
// 连接数据库，并查询
$result = mysqli_query($GLOBALS['conn'], $querySql);
// 查询当前传过来的用户名对应的题库信息
$obj = mysqli_fetch_object($result);

if (mysqli_num_rows($result) > 0 && ($obj->id != $id)) {
	echo "
<script>
    alert('该元素名称已被占用！请重新输入！');
    history.go(-1);
</script>";
} else {
	// 模板文件
	if (!empty($_FILES)) {
		/*
		$tmpName = $_FILES['upFile']['tmp_name'];     // 临时文件名称
		$suffix = explode(".", $_FILES['upFile']['name']);;         // 文件的后缀
		$path = '../static/templates';                       // 上传目录
		$name = $editName . "." . $suffix[1];
		*/
		
		$tmpName = $_FILES['upFile']['tmp_name'];     // 临时文件名称
		$suffix = explode(".", $_FILES['upFile']['name']);;         // 文件的后缀
		$path = '../asset/img';                       // 上传目录
		$fileName = $editName . "." . $suffix[1];
		if (move_uploaded_file($tmpName, $path . '/' . $fileName)) {
			// 后缀 suffix
			$now = date('Y-m-d H:i:s', time());
			$addSql = "UPDATE greeting_card_element SET e_name = '$editName' , saveAddress = '$path' , file_name = '$fileName' where id = '$id'";
			
			if (mysqli_query($GLOBALS['conn'], $addSql)) {
				echo "
            <script>
                alert('修改成功！');
                window.location.href=document.referrer;
            </script>
        ";
			} else {
				echo "
            <script>
                alert('修改失败！');
                history.back();
            </script>
        ";
			}
			// echo $name." 上传成功！";
		}
	} else {
		echo "<script>console.log('上传失败！')</script>";
	}
	
}

